package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.CartMapper;
import com.mapper.GoodsMapper;
import com.pojo.bo.GoodsBo;
import com.pojo.dto.DeleteCartDto;
import com.pojo.dto.OrderGoodsDto;
import com.pojo.entity.Cart;
import com.pojo.entity.Goods;
import com.pojo.vo.CartVo;
import com.service.ICartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : 杨世博
 * @date: Created in 15:32 2023/6/3
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Autowired
    CartMapper cartMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<CartVo> getCartList(Long uid) {
        List<Cart> cartList = cartMapper.getCartList(uid);
        List<CartVo> cartVos = new ArrayList<>();

        for (Cart cart : cartList) {
            CartVo cartVo = new CartVo();
            Goods goods = goodsMapper.selectById(cart.getGoodsId());
            BeanUtils.copyProperties(cart,cartVo);
            cartVo.setGoodsImage(goods.getGoodsImage());
            cartVo.setGoodsPrice(goods.getGoodsPrice());
            cartVo.setGoodsTitle(goods.getGoodsTitle());

            cartVos.add(cartVo);
        }
        return cartVos;
    }

    @Override
    public void addCart(Long uid, Integer goodsId, Integer cartNum) {
        QueryWrapper<Cart> qw = new QueryWrapper<>();
        qw.eq("uid", uid).eq("goods_id",goodsId);
        Cart cart1 = cartMapper.selectOne(qw);
        Cart cart = new Cart();
        cart.setUid(uid);
        cart.setGoodsId(goodsId);
        cart.setCartNum(cartNum);
        if (cart1==null){
            cartMapper.insert(cart);
        }else {
            updateGoodsNum(uid, goodsId, cartNum);
        }
    }

    @Override
    public List<CartVo> updateGoodsNum(Long uid, Integer goodsId, Integer cartNum) {
        Cart cart = new Cart();
        cart.setUid(uid);
        cart.setGoodsId(goodsId);
        cart.setCartNum(cartNum);
        UpdateWrapper<Cart> uw = new UpdateWrapper<>();
        uw.eq("uid", uid).eq("goods_id",goodsId);
        cartMapper.update(cart, uw);
        return getCartList(uid);
    }

    @Override
    public List<CartVo> deleteGoodsNum(Long uid, Integer goodsId) {
        QueryWrapper<Cart> qw = new QueryWrapper<>();
        qw.eq("uid",uid).eq("goods_id",goodsId);
        cartMapper.delete(qw);
        return getCartList(uid);
    }

    @Override
    public List<CartVo> deleteGoods(OrderGoodsDto orderGoodsDto) {

        System.out.println("\n\n\n\n\n\n\n\n"+orderGoodsDto+"\n\n\n\n\n\n\n");

        List<GoodsBo> goodId = orderGoodsDto.getGoods();
        for (GoodsBo goodsBo : goodId) {
            QueryWrapper<Cart> qw = new QueryWrapper<>();
            qw.eq("uid",orderGoodsDto.getUid()).eq("goods_id",goodsBo.getGoodId());
            cartMapper.delete(qw);
        }
        return getCartList(orderGoodsDto.getUid());
    }
}
